package com.rtx.edi.system.menu.controller;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rtx.edi.system.menu.service.MenuPermissionService;
import com.rtx.model.dto.response.ResponseHelper;
import com.rtx.model.dto.response.ResponseModel;
import com.rtx.model.emums.EnumDataType;
import com.rtx.model.po.entity.system.MenuPermission;
import com.rtx.model.po.entity.system.User;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 系统权限信息表 (MenuPermission)表控制层
 */
@RestController
@RequestMapping("menuPermission")
@Api(tags = "系统权限信息表 ", value = "MenuPermission")
public class MenuPermissionController {

    /**
     * 服务对象
     */
    @Resource
    private MenuPermissionService menuPermissionService;

    /**
     * 分页查询
     *
     * @param menuPermission 筛选条件
     * @return 查询结果
     */
    @PostMapping("queryMenuPermissionByPage")
    @ApiOperation(value = "系统权限信息表 -分页查询", httpMethod = "POST")
    public com.rtx.model.dto.response.ApiResponse<Page<MenuPermission>> queryMenuPermissionByPage(@RequestBody MenuPermission menuPermission) {
        return ResponseHelper.success(menuPermissionService.queryMenuPermissionByPage(menuPermission));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "系统权限信息表 -查询单条数据", httpMethod = "POST")
    @RequestMapping(value = "/queryMenuPermissionById/{id}", method = RequestMethod.POST)
    public ResponseModel queryMenuPermissionById(@PathVariable("id") Long id) {
        return ResponseModel.successBean(this.menuPermissionService.queryMenuPermissionById(id));
    }

    /**
     * 新增数据
     *
     * @param menuPermission 实体
     * @return 新增结果
     */
    @ApiOperation(value = "系统权限信息表 -新增数据", httpMethod = "POST")
    @RequestMapping(value = "/insertMenuPermission", method = RequestMethod.POST)
    public ResponseModel insertMenuPermission(@RequestBody MenuPermission menuPermission) {
        return ResponseModel.successBean(this.menuPermissionService.insertMenuPermission(menuPermission));
    }

    /**
     * 批量删除数据
     *
     * @param ids 主键集合
     * @return 删除是否成功
     */
    @ApiOperation(value = "系统权限信息表 -批量删除数据", httpMethod = "POST")
    @RequestMapping("/batchDeleteMenuPermissionByIds/{ids}")
    public ResponseModel batchDeleteMenuPermissionByIds(@PathVariable Long[] ids) {
        return ResponseModel.successBean(this.menuPermissionService.batchDeleteMenuPermissionByIds(ids));
    }

    /**
     * 获取用户菜单权限表信息
     *
     * @return
     */
    @GetMapping("lists")
    @ApiOperation(value = "菜单按钮维护列表", notes = "菜单按钮维护列表")
    public ResponseModel list() throws Exception {

        return ResponseModel.successBean(menuPermissionService.lists());
    }

    /**
     * 获取用户菜单权限表信息
     *
     * @return
     */
    @ResponseBody
    @GetMapping("getUserPermissionMenuInfo")
    @ApiOperation(value = "获取用户菜单权限表信息", notes = "获取用户菜单权限表信息")
    public ResponseModel getUserPermissionMenuInfo() throws Exception {

        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        User user = (User) session.getAttribute(EnumDataType.USER.getValue());
        return ResponseModel.successBean(menuPermissionService.getUserPermissionMenuInfo(user.getId(), user.getUserLanguage()));
    }

    /**
     * 添加同级菜单
     *
     * @return
     */
    @ResponseBody
    @PostMapping("addSiblingMenu")
    @ApiOperation(value = "添加同级菜单", notes = "说明：添加同级菜单【parentId和levelName属性，则取上级菜单的参数】，添加下级菜单【parentId和levelName属性，则取选中菜单的参数】")
    public ResponseModel addSiblingMenu(@RequestBody MenuPermission menuPermission) {

        return ResponseModel.successBean(menuPermissionService.addSiblingMenu(menuPermission));
    }

    /**
     * 编辑数据
     *
     * @param menuPermission 实体
     * @return 编辑结果
     */
    @PostMapping("updateMenuPermission")
    @ApiOperation(value = "系统权限信息表 -编辑数据", httpMethod = "POST")
    public ResponseModel updateMenuPermission(@RequestBody MenuPermission menuPermission) {
        return ResponseModel.successBean(this.menuPermissionService.updateMenuPermission(menuPermission));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @PostMapping("deleteMenuPermissionById/{id}")
    @ApiOperation(value = "系统权限信息表 -删除数据", httpMethod = "POST")
    public ResponseModel deleteMenuPermissionById(@PathVariable Long id) {
        return ResponseModel.successBean(this.menuPermissionService.deleteMenuPermissionById(id));
    }

}

